#!/bin/bash -e

#---------------------------------------------------
# export environment variables of chip version
# !!! important, must be set the right chip version
#---------------------------------------------------
export CHIP_X86=x86
export CHIP_V5=v5
export CHIP_V536=v536
export CHIP_V3=v3
export CHIP_A40i=a40i
export CHIP_V833=v833
export CHIP_Android=android

export PLAN_X86=x86
export PLAN_V5=v5
export PLAN_V3=v3
export PLAN_A40I=a40i
export PLAN_ANDROID=android
export PLAN_V536=v536-Lindenis_SBC
export PLAN_V833=v833-lindenis

function print_lunch_menu()
{
	printf "\033[32m"
	echo "#######################################################################################"
	echo "# lindsdk chip platform choice: "
	echo "#     0. x86                    [limited]"
	echo "#     1. v5                     [Not Ready]"
	echo "#     2. v3                     [Not Ready]"
	echo "#     3. a40i                   [Not Ready]"
	echo "#     4. android"
	echo "#     5. v536"
	echo "#     6. v833"
	echo "#######################################################################################"
	printf "\033[0m"
}

if [ "$1" ] ; then
	answer=$1
else
	print_lunch_menu
	echo -n "Which would you like? choose[0~6]: "
	read answer
fi

if [ $answer == 0 ];then
	export CHIP=$CHIP_X86
	export PLAN_NAME=$PLAN_X86
elif [ $answer == 1 ];then
	export CHIP=$CHIP_V5
	export PLAN_NAME=$PLAN_V5
elif [ $answer == 2 ];then
	export CHIP=$CHIP_V3
	export PLAN_NAME=$PLAN_V3
elif [ $answer == 3 ];then
	export CHIP=$CHIP_A40i
	export PLAN_NAME=$PLAN_A40I
elif [ $answer == 4 ];then
	export CHIP=$CHIP_Android
	export PLAN_NAME=$PLAN_ANDROID
elif [ $answer == 5 ];then
	export CHIP=$CHIP_V536
	export PLAN_NAME=$PLAN_V536
elif [ $answer == 6 ];then
	export CHIP=$CHIP_V833
	export PLAN_NAME=$PLAN_V833
else
    echo "fatal error: unknown choice: $answer"
	return 1
fi

#---------------------------------------------------
# export environment variables of work directory
#---------------------------------------------------
export TOP_DIR=`pwd`
export OUT_DIR=$TOP_DIR/out/$PLAN_NAME
export FRAMEWORK_DIR=$TOP_DIR/framework
export DEPENDS_DIR=$TOP_DIR/depends/$PLAN_NAME
export SAMPLE_DIR=$TOP_DIR/sample

if [ ! -d $OUT_DIR/bin ];then
mkdir -p $OUT_DIR/bin
fi

if [ ! -d $OUT_DIR/lib ];then
mkdir -p $OUT_DIR/lib
fi

if [ ! -d $OUT_DIR/include ];then
mkdir -p $OUT_DIR/include
fi


function croot()
{
	cd $TOP_DIR
}

function cout()
{
	cd $OUT_DIR
}

function cdepens()
{
	cd $DEPENDS_DIR
}

#---------------------------------------------------
# export environment variables of compile
# !!! important, must be set the right path of toolchains
#---------------------------------------------------
if [ $CHIP == $CHIP_X86 ];then
    export TARGET_OS=linux

    export CC=gcc
    export CXX=g++
    export STRIP=strip
    export AR=ar
    export LD=ld
    export LINK=$CXX
    export INSTALL=install

    printf "\033[32m"
    echo "Build for x86, got it."
    printf "\033[0m"
    return
elif [ $CHIP == $CHIP_V5 ];then
    TOOLCHAINS_DIR=$TOP_DIR/toolchains/gcc-linaro-5.3.1-2016.05-x86_64_arm-linux-gnueabi/bin
	if [ ! -d $TOOLCHAINS_DIR ];then
		if [ ! -f $TOP_DIR/toolchains/gcc-linaro-5.3.1-2016.05-x86_64_arm-linux-gnueabi.tar.xz ];then
			cat $TOP_DIR/toolchains/gcc-linaro-5.3.1-2016.05-x86_64_arm-linux-gnueabi.tar.xz_* > \
				$TOP_DIR/toolchains/gcc-linaro-5.3.1-2016.05-x86_64_arm-linux-gnueabi.tar.xz
		fi
		tar -xf $TOP_DIR/toolchains/gcc-linaro-5.3.1-2016.05-x86_64_arm-linux-gnueabi.tar.xz -C $TOP_DIR/toolchains/
	fi
    export CROSS_COMPILE=arm-linux-gnueabi
    export TARGET_OS=linux
elif [ $CHIP == $CHIP_V536 ];then
    TOOLCHAINS_DIR=$TOP_DIR/toolchains/toolchain-sunxi-musl/toolchain/bin
    if [ ! -d $TOOLCHAINS_DIR ];then
        tar -xzf $TOP_DIR/toolchains/toolchain-sunxi-musl.tar.gz -C $TOP_DIR/toolchains/
    fi
    export STAGING_DIR=$OUT_DIR
    export CROSS_COMPILE=arm-openwrt-linux-muslgnueabi
    export TARGET_OS=linux
elif [ $CHIP == $CHIP_V3 ];then
    TOOLCHAINS_DIR=$TOP_DIR/toolchains/arm-linux-androideabi-4.9/bin
    if [ ! -d $TOOLCHAINS_DIR ];then
        if [ ! -f $TOP_DIR/toolchains/arm-linux-androideabi-4.9.tar.bz2 ];then
            cat $TOP_DIR/toolchains/arm-linux-androideabi-4.9.tar.bz2_* > \
                $TOP_DIR/toolchains/arm-linux-androideabi-4.9.tar.bz2
        fi
        tar -xf $TOP_DIR/toolchains/arm-linux-androideabi-4.9.tar.bz2 -C $TOP_DIR/toolchains/
    fi
    export CROSS_COMPILE=arm-linux-androideabi
    export TARGET_OS=android
elif [ $CHIP == $CHIP_A40i ];then
    export CROSS_COMPILE=arm-linux-gnueabihf
    export TARGET_OS=linux
elif [ $CHIP == $CHIP_V833 ];then
    TOOLCHAINS_DIR=$TOP_DIR/toolchains/toolchain-sunxi-musl/toolchain/bin
    if [ ! -d $TOOLCHAINS_DIR ];then
        tar -xzf $TOP_DIR/toolchains/toolchain-sunxi-musl.tar.gz -C $TOP_DIR/toolchains/
    fi
    export STAGING_DIR=$OUT_DIR
    export CROSS_COMPILE=arm-openwrt-linux-muslgnueabi
    export TARGET_OS=linux
elif [ $CHIP == $CHIP_Android ];then
    TOOLCHAINS_DIR=$TOP_DIR/toolchains/arm-linux-androideabi-4.9/bin
    if [ ! -d $TOOLCHAINS_DIR ];then
		if [ ! -f $TOP_DIR/toolchains/arm-linux-androideabi-4.9.tar.bz2 ];then
			cat $TOP_DIR/toolchains/arm-linux-androideabi-4.9.tar.bz2_* > \
				$TOP_DIR/toolchains/arm-linux-androideabi-4.9.tar.bz2
		fi
        tar -xf $TOP_DIR/toolchains/arm-linux-androideabi-4.9.tar.bz2 -C $TOP_DIR/toolchains/
    fi
    export CROSS_COMPILE=arm-linux-androideabi
    export TARGET_OS=android
else
    echo "fatal error: unknown chip: $CHIP"
fi

export PATH=$PATH:$TOOLCHAINS_DIR

export CC=$CROSS_COMPILE-gcc
export CXX=$CROSS_COMPILE-g++
export STRIP=$CROSS_COMPILE-strip
export AR=$CROSS_COMPILE-ar
export LD=$CROSS_COMPILE-ld
export LINK=$CXX
export INSTALL=install

#---------------------------------------------------
# export the project environment variables
#---------------------------------------------------
CONFIG_FILE=config_$PLAN_NAME.sh
source $TOP_DIR/build/$CONFIG_FILE
source $TOP_DIR/build/user_env.sh

#---------------------------------------------------
# now the main config is done
#---------------------------------------------------
printf "\033[32m"
echo "#######################################################################################"
echo "# lindsdk config: "
echo "#     chip            : $CHIP"
echo "#     plan            : $PLAN_NAME"
echo "#     CROSS_COMPILE   : $CROSS_COMPILE"
echo "#     TARGET_OS       : $TARGET_OS"
echo "#     toolchains path : $TOOLCHAINS_DIR"
echo "#     sdk path        : $SDK_PATH"
echo "#######################################################################################"
printf "\033[0m"

#---------------------------------------------------
# export functions
#---------------------------------------------------
